import {
  SET_FULL_PLAYER,
  SET_MINI_PLAYER,
  SET_PLAYING,
  SET_MODETYPE,
  SONG_TYPE,
  SET_SONG_LYRIC,
  DEL_SINGAL_SONG,
  SET_CURRENT_INDEX,
  SET_PLAYING_TIME,
  SET_FAVORITE_SONG,
  SET_FAVORITE_LIST,
  SET_HISTORY_LIST,
  SET_HISTORY_SONG
} from './mutations-type'
import { getSongDetail, getSongLyric, getSongUrl } from '../api/index'
export default {
  setFullPlayer ({ commit }, flag) {
    commit(SET_FULL_PLAYER, flag)
  },
  setMiniPlayer ({ commit }, flag) {
    commit(SET_MINI_PLAYER, flag)
  },
  setPlaying ({ commit }, flag) {
    commit(SET_PLAYING, flag)
  },
  setModeType ({ commit }, flag) {
    commit(SET_MODETYPE, flag)
  },
  async getSongs ({ commit }, id) {
    if (typeof id === 'number') {
      id = [id]
    }
    let result = await getSongDetail({ ids: [...id].join(',') })
    let urls = await getSongUrl({ id: [...id].join(',') })
    result = result.data
    urls = urls.data.data
    const list = []
    result.songs.forEach(function (value, index) {
      const obj = {}
      let singer = ''
      for (let i = 0; i < urls.length; i++) {
        const item = urls[i]
        if (value.id === item.id) {
          obj.url = item.url
        }
      }
      obj.name = value.name
      obj.id = value.id
      value.ar.forEach(function (value, index) {
        if (index === 0) {
          singer = value.name
        } else {
          singer += '-' + value.name
        }
      })
      obj.singer = singer
      obj.picUrl = value.al.picUrl
      list.push(obj)
    })
    commit(SONG_TYPE, list)
  },
  async setSongLyric ({ commit }, id) {
    let result = await getSongLyric({ id: id })
    result = result.data.lrc.lyric
    result = parseLyric(result)
    commit(SET_SONG_LYRIC, result)
  },
  delSingalSong ({ commit }, index) {
    commit(DEL_SINGAL_SONG, index)
  },
  setCurrentIndex ({ commit }, index) {
    commit(SET_CURRENT_INDEX, index)
  },
  setPlayingTime ({ commit }, time) {
    commit(SET_PLAYING_TIME, time)
  },
  setFavoriteList ({ commit }, song) {
    commit(SET_FAVORITE_SONG, song)
  },
  setFavoriteListByLocal ({ commit }, list) {
    commit(SET_FAVORITE_LIST, list)
  },
  setHistorySong ({ commit }, song) {
    commit(SET_HISTORY_SONG, song)
  },
  setHistoryList ({ commit }, list) {
    commit(SET_HISTORY_LIST, list)
  }
}
// 格式化歌词方法
function parseLyric (lrc) {
  const lyrics = lrc.split('\n')
  // [00:00.000] 作曲 : 林俊杰
  // 1.定义正则表达式提取[00:00.000]
  const reg1 = /\[\d*:\d*\.\d*\]/g
  // 2.定义正则表达式提取 [00
  const reg2 = /\[\d*/i
  // 3.定义正则表达式提取 :00
  const reg3 = /:\d*/i
  // 4.定义对象保存处理好的歌词
  const lyricObj = {}
  lyrics.forEach(function (lyric) {
    // 1.提取时间
    let timeStr = lyric.match(reg1)
    if (!timeStr) { return }
    // 全局匹配结果timeStr为数组
    timeStr = timeStr[0]
    // 2.提取分钟
    const minStr = timeStr.match(reg2)[0].substr(1)
    // 3.提取秒钟
    const secondStr = timeStr.match(reg3)[0].substr(1)
    // 4.合并时间, 将分钟和秒钟都合并为秒钟
    const time = parseInt(minStr) * 60 + parseInt(secondStr)
    // 5.处理歌词
    const text = lyric.replace(reg1, '').trim()
    // 6.保存数据
    lyricObj[time] = text
  })
  return lyricObj
}
